<?php
@session_start();
class Product extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index(){
       // echo 'in';exit;
         $sql = new sql();
         $sql->connect();
         $this->view->category = $sql->fetchAll("tbl_category",  array("cat_status" => 1));
         $limit = 12;
         
         
            
         
         if(array_key_exists('2', getId())){
             $GETID = getId();
             $page = $GETID[2];
         }else{
            $page = (int) (GET("page") ? GET("page") : 1 );
         }
         if(array_key_exists('search_btn',$_POST)){
            if($_POST['product_code'] != ''){
                $search = " and prd_code LIKE '%".$_POST['product_code']."%'";
                $_SESSION['search_key_word'] = $_POST['product_code'];
            }else{
                $search = " and prd_showcase =1";
                $_SESSION['search_key_word'] = "";
            }
         }else{
             $search = " and prd_showcase =1";
             $_SESSION['search_key_word'] = "";
         }
         
         if (array_key_exists('1', getId())) {
            $GETID = getId();
            $cat_alias = "";
            if (is_numeric($GETID[1])){
                $page = $GETID[1];
                $statement = "tbl_product as prd join tbl_category as cat on cat.cat_id = prd.prd_cat_id  
                 where prd_status = 1 ".$search."  order by prd_id desc";
            }else{
                $cat_alias = $GETID[1];
                $GetCategoryId = $sql->fetch("tbl_category", array("cat_alias" => $GETID[1]));
                $statement = "tbl_product as prd join tbl_category as cat on cat.cat_id = prd.prd_cat_id  
                 where prd_status = 1 ".$search." and prd_cat_id = " . $GetCategoryId['cat_id'] . "
                 order by prd_id desc";
            }
         } else {
             $cat_alias = "";
             $statement = "tbl_product as prd join tbl_category as cat on cat.cat_id = prd.prd_cat_id  
                 where prd_status = 1 ".$search." order by prd_id desc";
         }
         
         $this->view->category_name = $cat_alias;
         $this->view->statement = $statement;
         $this->view->limit = $limit;
         $this->view->page = $page;
         $this->view->product = $sql->PagingNationFetch($statement, $page, $limit);
         $this->view->render('product');
    }
}
